﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BoutiquePOS.DAL.DAO;


namespace BoutiquePOS.DAL.Gateway
{
    public class StoreGateway:DbGateway
    {
        public string SaveStore(Store aStore)
        {
            try
            {
                string insert = String.Format("INSERT INTO PosStore VALUES( @name)");
                ConnectionObj.Open();
                CommandObj.Parameters.Clear();
                CommandObj.CommandText = insert;
                CommandObj.Parameters.Add("@name", SqlDbType.VarChar);
                CommandObj.Parameters["@name"].Value =aStore.StoreName;
                CommandObj.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Store Couldn't Saved",ex);
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return "Save Sucessfully";
        }
        public List<Store> GetAllStore()
        {
            List<Store> stores = new List<Store>();
            try
            {
                ConnectionObj.Open();
                string query = String.Format("SELECT * FROM PosStore");

                CommandObj.CommandText = query;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Store aStore = new Store();
                    aStore.StoreId = Convert.ToInt32(reader["StoreId"]);
                    aStore.StoreName = reader["StoreName"].ToString();
                    stores.Add(aStore);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Error occurred during Store loading from your system.", exception);
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return stores;
        }
        public Store FindStore(int storeId)
        {
            try
            {
                string search = String.Format("Select * FROM PosStore WHERE StoreId=@StoreId");
                ConnectionObj.Open();
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@StoreId", SqlDbType.Int);
                CommandObj.Parameters["@StoreId"].Value = storeId;
                CommandObj.CommandText = search;
                SqlDataReader sqlDataReader = CommandObj.ExecuteReader();
                Store Store = new Store();
                if (sqlDataReader != null)
                {
                    while (sqlDataReader.Read())
                    {
                        Store.StoreId = Convert.ToInt32(sqlDataReader["StoreId"]);
                        Store.StoreName = sqlDataReader["StoreName"].ToString();
                        return Store;
                    }
                }
                return null;
            }

            catch (Exception exception)
            {
                throw new Exception("Could not Found Store", exception);
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
        }

        public string DeleteStoreById(int storeId)
        {
            int i = 0;
            try
            {
                ConnectionObj.Open();
                string delete = String.Format("DELETE FROM PosStore WHERE storeId='{0}'", storeId);
                CommandObj.CommandText = delete;
                i = CommandObj.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw new Exception("Database Error");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            if (i != 0)
            {
                return "Delete Successfully";
            }
            else
            {
                return "Store Id does not found";
            }
        }

        public List<Store> GetAllStoreByCodeOrName(int i, string storeId)
        {
            List<Store> aStores = new List<Store>();
            string select = "";

            try
            {
                ConnectionObj.Open();
                if (i == 0)
                {

                    select = String.Format("SELECT * FROM PosStore WHERE storeId ='{0}'", Convert.ToInt32(storeId));
                }
                if (i == 1)
                {
                    storeId = storeId + "%";
                    select = String.Format("SELECT * FROM PosStore WHERE storeName LIKE '" + storeId + "'");
                }

                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Store store = new Store();
                    store.StoreName = reader[1].ToString();
                    store.StoreId = Convert.ToInt32(reader[0]);
                    aStores.Add(store);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }
            }
            return aStores;
        }



    }
}